package com.easemob.helpdesk.activity.chat;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.easemob.helpdesk.activity.BaseActivityV2;
import com.easemob.helpdesk.activity.ContextMenu;
import com.easemob.helpdesk.utils.CommonUtils;
import com.easemob.helpdesk.utils.DownloadManager;
import com.easemob.helpdesk.widget.photoview.PhotoView;
import com.easemob.helpdesk.widget.photoview.PhotoViewAttacher;
import com.housekeeperapp.R;

import java.io.File;
import java.lang.ref.SoftReference;

import okhttp3.Request;

/**
 * Created by wsj on 2018/6/6.
 */

public class MsgShowBigImage extends BaseActivityV2 {
    private PhotoView image;
    private int default_res = R.drawable.default_image;
    private ImageView down_icon;

    private String localFilePath;
    private ProgressBar loadLocalPb;
    private SoftReference<Bitmap> softBitmap = null;
    private String remoteURL = null;
    private String message;
    private String msgid;

    public static final int REQUEST_CODE_CONTEXT_MENU = 0x011;
    public static final int RES_CODE_SAVE = 0x022;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_big_image);
        image = (PhotoView) findViewById(R.id.image);
        loadLocalPb = (ProgressBar) findViewById(R.id.pb_load_local);
        down_icon = (ImageView) findViewById(R.id.down_icon);

        Intent gIntent = getIntent();

        message = gIntent.getStringExtra("picpath");
        msgid = gIntent.getStringExtra("msgid");

        //HDImageMessageBody HDImageMessageBody = (com.hyphenate.kefusdk.entity.HDImageMessageBody) message.getBody();

        localFilePath = DownloadManager.getResourcePath(msgid);
        remoteURL = message;

        //本地存在，直接显示本地的图片
        if (localFilePath != null && new File(localFilePath).exists()) {
            Bitmap bitmap = CommonUtils.getScaleBitmap(MsgShowBigImage.this, localFilePath);
            softBitmap = new SoftReference<Bitmap>(bitmap);
            if (softBitmap.get() != null) {
                image.setImageBitmap(softBitmap.get());
            } else {
                image.setImageResource(default_res);
            }
        } else if (remoteURL != null) {
            downloadAttachement();
        } else {
            image.setImageResource(default_res);
        }

        down_icon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MsgShowBigImage.this, "已成功保存到"+DownloadManager.getSDPATH(), Toast.LENGTH_LONG).show();
            }
        });

        image.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                Intent intent = new Intent(MsgShowBigImage.this, ContextMenu.class);
                intent.putExtra("type", ContextMenu.TYPE_CONTEXT_MENU_IMAGE);

                startActivityForResult(intent, REQUEST_CODE_CONTEXT_MENU);
                return false;
            }
        });

        image.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {

            @Override
            public void onPhotoTap(View view, float x, float y) {
                finish();
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_CONTEXT_MENU) {
            if (resultCode == RES_CODE_SAVE) {
                Toast.makeText(this, "已成功保存到"+DownloadManager.getSDPATH(), Toast.LENGTH_LONG).show();
            }
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (softBitmap != null) {
            softBitmap.clear();
            softBitmap = null;
        }
    }

    private void downloadAttachement() {
        loadLocalPb.setVisibility(View.VISIBLE);

        DownloadManager.downloadFile(remoteURL, msgid, new DownloadManager.ResultCallback() {
            @Override
            public void onError(Request request, Exception e) {
                File localFile = new File(localFilePath);
                if (localFile.exists()) {
                    localFile.delete();
                }
                if (isFinishing()) {
                    return;
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        loadLocalPb.setVisibility(View.GONE);
                        image.setImageResource(default_res);

                    }
                });
            }

            @Override
            public void onResponse(Object response) {
                if (isFinishing()) {
                    return;
                }

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        loadLocalPb.setVisibility(View.GONE);
                        localFilePath = DownloadManager.getResourcePath(msgid);

                        Bitmap bitmap = CommonUtils.getScaleBitmap(MsgShowBigImage.this, localFilePath);
                        softBitmap = new SoftReference<Bitmap>(bitmap);
                        if (softBitmap.get() != null) {
                            image.setImageBitmap(softBitmap.get());
                        } else {
                            image.setImageResource(default_res);
                        }
                    }
                });
            }

            @Override
            public void onProgress(double total, double current) {

            }
        });

//        HDClient.getInstance().chatManager().downloadAttachment(message);
    }

    @Override
    public void onBackPressed() {
        finish();
    }
}
